<!Doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>C++ 模板详解 | 菜鸟教程</title>

  <meta name='robots' content='max-image-preview:large' />
<link rel='stylesheet' id='classic-theme-styles-css' href='../0/classic-themes.min.css?ver=6.2' type='text/css' media='all' />
<link rel="canonical" href="../w3cnote/c-templates-detail.html" />
<meta name="keywords" content="C++ 模板详解">
<meta name="description" content="模板是C++支持参数化多态的工具，使用模板可以使用户为类或者函数声明一种一般模式，使得类中的某些数据成员或者成员函数的参数、返回值取得任意类型。 模板是一种对类型进行参数化的工具； 通常有两种形式：函数模板和类模板； 函数模板针对仅参数类型不同的函数； 类模板针对仅数据成员和成员函数类型不同的类。 使用模板的目的就是能够让程序员编写与类型无关的代码。比如编写了一个交换两个整型int 类型的swap函数，这个函数就只能实现int 型，对d..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico">
	<link rel="stylesheet" href="../0/style.css?v=1.170" type="text/css" media="all" />	
	<link rel="stylesheet" href="../0/font-awesome.min.css" media="all" />	
  <!--[if gte IE 9]><!-->
  <script src=""></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src=""></script>
     <script src=""></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
      
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="../">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        
    <div class="col search search-desktop last">
      <div class="search-input" >
      <form action="//www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……"  autocomplete="off" style="height: 44px;">
      </form>
      
      </div>
    </div>
  </div>
</div>



<!-- 导航栏 -->
<div class="container navigation">
    <div class="row">
        <div class="col nav">
            

                        <ul class="pc-nav" id="note-nav">
                <li><a href="../">首页</a></li>
                <li><a href="../w3cnote">笔记首页</a></li>
                <li><a href="../w3cnote/android-tutorial-intro.html" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" title="ES6 教程">ES6 教程</a></li>
                <li><a href="../w3cnote/ten-sorting-algorithm.html" title="排序算法">排序算法</a></li>
                <li><a href="../w3cnote/hadoop-tutorial.html" title="Hadoop 教程">Hadoop</a></li>
                <li><a href="../w3cnote/zookeeper-tutorial.html" title="Zookeeper 教程">Zookeeper</a></li>
                <li><a href="../w3cnote/verilog-tutorial.html" title="Verilog 教程">Verilog</a></li>
                <li><a href="../w3cnote_genre/code" title="编程技术">编程技术</a></li> 
                <li><a href="../w3cnote_genre/coderlife" title="程序员人生">程序员人生</a></li>
                
                <!--<li><a href="javascript:;" class="runoob-pop">登录</a></li>
                
                
                        <li>
                <a style="font-weight:bold;" href="../linux/linux-tutorial.html#yunserver" target="_blank" onclick="_hmt.push(['_trackEvent', 'aliyun', 'click', 'aliyun'])" title="kkb">云服务器</a>
                </li>
                <li><a href="http://gk.link/a/104mQ" target="_blank" style="font-weight: bold;"onclick="_hmt.push(['_trackEvent', '极客时间', 'click', 'jike'])" title="我的圈子">极客时间</a></li>
            
                
                <li><a target="_blank" href="../shoppinglist" rel="nofollow">知识店铺</a></li> 
        -->
            </ul>
                        
              
            <ul class="mobile-nav">
                <li><a href="../w3cnote">首页</a></li>
                <li><a href="../w3cnote_genre/android" target="_blank" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" target="_blank" title="ES6 教程">ES6</a></li>
                <li><a href="../w3cnote_genre/joke" target="_blank" title="程序员笑话">逗乐</a></li>
                
                <a href="javascript:void(0)" class="search-reveal">Search</a> 
            </ul>
            
        </div>
    </div>
</div>


<!--  内容  -->
<div class="container main">
	<div class="row">

		<div class="col middle-column big-middle-column">
	 			<div class="article">
			<div class="article-heading">
				<h2>C++ 模板详解</h2>				<h3><em>分类</em> <a href="../w3cnote_genre/code" title="编程技术" >编程技术</a> </h3>
			</div>
			<div class="article-body note-body">
				<div class="article-intro">
					
<p>模板是C++支持参数化多态的工具，使用模板可以使用户为类或者函数声明一种一般模式，使得类中的某些数据成员或者成员函数的参数、返回值取得任意类型。</p>
<p>模板是一种对类型进行参数化的工具；</p>
<p>通常有两种形式：函数模板和类模板；</p>
<p>函数模板针对仅参数类型不同的函数；</p>
<p>类模板针对仅数据成员和成员函数类型不同的类。</p>
<p>使用模板的目的就是能够让程序员编写与类型无关的代码。比如编写了一个交换两个整型int 类型的swap函数，这个函数就只能实现int 型，对double，字符这些类型无法实现，要实现这些类型的交换就要重新编写另一个swap函数。使用模板的目的就是要让这程序的实现与类型无关，比如一个swap模板函数，即可以实现int 型，又可以实现double型的交换。模板可以应用于函数和类。下面分别介绍。</p>
<p><strong>注意：</strong>模板的声明或定义只能在全局，命名空间或类范围内进行。即不能在局部范围，函数内进行，比如不能在main函数中声明或定义一个模板。</p>
<hr><h2>
一、函数模板通式</h2>
<p><strong>1、函数模板的格式：</strong></p>
<pre>template &lt;class 形参名，class 形参名，......&gt; 返回类型 函数名(参数列表)
{
    函数体
}</pre>
<p>其中template和class是关见字，class可以用typename 关见字代替，在这里typename 和class没区别，<>括号中的参数叫模板形参，模板形参和函数形参很相像，模板形参不能为空。一但声明了模板函数就可以用模板函数的形参名声明类中的成员变量和成员函数，即可以在该函数中使用内置类型的地方都可以使用模板形参名。模板形参需要调用该模板函数时提供的模板实参来初始化模板形参，一旦编译器确定了实际的模板实参类型就称他实例化了函数模板的一个实例。比如swap的模板函数形式为:</p>

<pre>template &lt;class T&gt; void swap(T&amp; a, T&amp; b){}，</pre>

<p>当调用这样的模板函数时类型T就会被被调用时的类型所代替，比如<strong>swap(a,b)</strong>其中<strong>a</strong>和<strong>b</strong>是<strong>int</strong> 型，这时模板函数swap中的形参<strong>T</strong>就会被<strong>int</strong> 所代替，模板函数就变为<strong>swap(int &amp;a, int &amp;b)</strong>。而当<strong>swap(c,d)</strong>其中<strong>c</strong>和<strong>d</strong>是<strong>double</strong>类型时，模板函数会被替换为<strong>swap(double &amp;a, double &amp;b)</strong>，这样就实现了函数的实现与类型无关的代码。</p>

<p>　　2、注意：对于函数模板而言不存在 <strong>h(int,int)</strong> 这样的调用，<span><strong>不能在函数调用的参数中指定模板形参的类型，对函数模板的调用应使用<span>实参推演</span>来进行</strong></span>，即只能进行<strong> h(2,3)</strong> 这样的调用，或者<strong>int a, b; h(a,b)</strong>。</p>
<p>
函数模板的示例演示将在下文中涉及！</p>
<hr><h2>
二、类模板通式
</h2><p>1、类模板的格式为：</p>

<pre>template&lt;class  形参名，class 形参名，&hellip;&gt;   class 类名
{ ... };</pre>

<p>类模板和函数模板都是以template开始后接模板形参列表组成，模板形参不能为空，一但声明了类模板就可以用类模板的形参名声明类中的成员变量和成员函数，即可以在类中使用内置类型的地方都可以使用模板形参名来声明。比如</p>

<pre>template&lt;class T&gt; class A{public: T a; T b; T hy(T c, T &amp;d);};</pre>

<p>在类A中声明了两个类型为T的成员变量a和b，还声明了一个返回类型为T带两个参数类型为T的函数hy。</p>

<p>　　2、类模板对象的创建：比如一个模板类<strong>A</strong>，则使用类模板创建对象的方法为<strong>A&lt;int&gt; m;</strong>在类<strong>A</strong>后面跟上一个<strong>&lt;&gt;</strong>尖括号并在里面填上相应的类型，这样的话类<strong>A</strong>中凡是用到模板形参的地方都会被<strong>int</strong> 所代替。当类模板有两个模板形参时创建对象的方法为<strong>A&lt;int, double&gt; m;</strong>类型之间用逗号隔开。</p>
<p>　　3、对于类模板，模板形参的类型必须在类名后的尖括号中明确指定。比如<strong>A&lt;2&gt; m;</strong>用这种方法把模板形参设置为<strong>int</strong>是错误的（<strong>编译错误：error C2079: 'a' uses undefined class 'A&lt;int&gt;'</strong>），<span><strong>类模板形参不存在实参推演的问题。</strong></span>也就是说不能把整型值<strong>2</strong>推演为<strong>int</strong> 型传递给模板形参。要把类模板形参调置为<strong>int</strong> 型必须这样指定<strong>A&lt;int&gt; m</strong>。</p>
<p>4、在类模板外部定义成员函数的方法为：</p>

<pre>template&lt;模板形参列表&gt; 函数返回类型 类名&lt;模板形参名&gt;::函数名(参数列表){函数体}，</pre>

<p>比如有两个模板形参T1，T2的类A中含有一个void h()函数，则定义该函数的语法为：</p>

<pre>template&lt;class T1,class T2&gt; void A&lt;T1,T2&gt;::h(){}。</pre>

<p><strong>注意：</strong>当在类外面定义类的成员时template后面的模板形参应与要定义的类的模板形参一致。</p>

<p>5、再次提醒注意：模板的声明或定义只能在全局，命名空间或类范围内进行。即不能在局部范围，函数内进行，比如不能在<strong>main</strong>函数中声明或定义一个模板。</p>

 
<hr><h2>
三、模板的形参</h2>
<p>有三种类型的模板形参：类型形参，非类型形参和模板形参。</p>
<h3>
1、类型形参</h3>
<p>1.1 、类型模板形参：<strong><span>类型形参由关见字class或typename后接说明符构成</span></strong>，如<strong>template&lt;class T&gt; void h(T a){}</strong>;其中<strong>T</strong>就是一个类型形参，类型形参的名字由用户自已确定。<span ><strong>模板形参表示的是一个未知的类型</strong></span>。模板类型形参可作为类型说明符用在模板中的任何地方，与内置类型说明符或类类型说明符的使用方式完全相同，即可以用于指定返回类型，变量声明等。</p>
<p><strong><span style="color: #ff0000;">作者原版：</span><span style="color: #ff0000;">1.2、</span></strong>不能为同一个模板类型形参指定两种不同的类型，比如<strong>template&lt;class T&gt;void h(T a, T b){}，</strong>语句调用<strong>h(2, 3.2)</strong>将出错，因为该语句给同一模板形参<strong>T</strong>指定了两种类型，第一个实参<strong>2</strong>把模板形参T指定为<strong>int</strong>，而第二个实参<strong>3.2</strong>把模板形参指定为<strong>double</strong>，两种类型的形参不一致，会出错。<span style="color: #ff0000; background-color: #99ccff;"><strong>（针对函数模板）</strong></span></p>

<p>作者原版：1.2针对函数模板是正确的，但是忽略了类模板。下面将对类模板的情况进行补充。</p>
<p><strong><span style="color: #ff0000;">本人添加1.2补充版（针对于类模板）、</span></strong>当我们声明类对象为：<strong>A&lt;int&gt; a</strong>，比如<strong>template&lt;class T&gt;T g(T a, T b){}</strong>，语句调用<strong>a.g(2, 3.2)</strong>在编译时不会出错，但会有警告，因为在声明类对象的时候已经将<strong>T</strong>转换为<strong>int</strong>类型，而第二个实参<strong>3.2</strong>把模板形参指定为<strong>double</strong>，在运行时，会对<strong>3.2</strong>进行强制类型转换为<strong>3</strong>。当我们声明类的对象为：<strong>A&lt;double&gt; a</strong>,此时就不会有上述的警告，因为从<strong>int</strong>到<strong>double</strong>是自动类型转换。</p>
<h3>演示示例１：
</h3>

<div class="example">
<h2 class="example">TemplateDemo.h</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_HXX</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_HXX</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">A</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-prepro">#endif</span><span class="hl-prepro"></span><span class="hl-code">
 
　　</span><span class="hl-identifier">TemplateDemo</span><span class="hl-code">.</span><span class="hl-identifier">cpp</span><span class="hl-code">
 
</span><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;::</span><span class="hl-identifier">A</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">a</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">a</span><span class="hl-code">.</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-number">.2</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>编译结果：</p>

<pre>--------------------Configuration: TemplateDemo - Win32 Debug--------------------
Compiling...
TemplateDemo.cpp
G:\C++\CDaima\TemplateDemo\TemplateDemo.cpp(12) : warning C4244: 'argument' : conversion from 'const double' to 'int', possible loss of data

TemplateDemo.obj - 0 error(s), 1 warning(s)</pre>

<p>运行结果：　<strong>5</strong>　</p>　

<p>我们从上面的测试示例中可以看出，并非作者原作中的那么严密！此处仅是本人跟人测试结果！请大家本着实事求是的态度，自行验证！</p>

 
<h3>
2、非类型形参</h3>
<p>　　　　2.1 、非类型模板形参：<span style="color: #0000ff;"><strong>模板的非类型形参也就是内置类型形参</strong></span>，如<strong>template&lt;class T, int a&gt; class B{};</strong>其中<strong>int a</strong>就是非类型的模板形参。</p>
<p>　　　　2.2、 非类型形参在模板定义的内部是常量值，也就是说非类型形参在模板的内部是常量。</p>
<p>　　　　<strong>2.3、 <span style="color: #0000ff;">非类型<strong><span style="color: #0000ff;">模板</span></strong>的形参只能是整型，指针和引用</span>，像double，String, String **这样的类型是不允许的。</strong>但是<strong>double &amp;，double *，</strong>对象的引用或指针是正确的。</p>
<p>　　　　2.4、 <strong>调用非类型模板形参的实参必须是一个常量表达式</strong>，即他必须能在编译时计算出结果。</p>
<p>　　　　2.5 、注意：<strong>任何局部对象，局部变量，局部对象的地址，局部变量的地址都不是一个常量表达式，都不能用作非类型模板形参的实参</strong>。全局指针类型，全局变量，全局对象也不是一个常量表达式，不能用作非类型模板形参的实参。</p>
<p>　　　　2.6、 <span style="color: #0000ff;"><strong>全局变量的地址或引用，全局对象的地址或引用const类型变量是常量表达式，可以用作非类型模板形参的实参</strong></span>。</p>
<p>　　　　2.7 、<strong>sizeof</strong>表达式的结果是一个常量表达式，也能用作非类型模板形参的实参。</p>
<p>　　　　2.8 、当模板的形参是整型时调用该模板时的实参必须是整型的，且在编译期间是常量，比如<strong>template &lt;class T, int a&gt; class A{};</strong>如果有<strong>int b，这时A&lt;int, b&gt; m;</strong>将出错，因为<strong>b</strong>不是常量，如果<strong>const int b，这时A&lt;int, b&gt; m</strong>;就是正确的，因为这时<strong>b</strong>是常量。</p>
<p>　　　　<strong>2.9 、<span style="color: #0000ff;">非类型形参一般不应用于函数模板中</span></strong>，比如有函数模板<strong>template&lt;class T, int a&gt; void h(T b){}</strong>，若使用<strong>h(2)</strong>调用会出现无法为非类型形参a推演出参数的错误，对这种模板函数可以用显示模板实参来解决，如用h&lt;int, 3&gt;(2)这样就把非类型形参a设置为整数3。显示模板实参在后面介绍。</p>
<p>　　　　2.10、 非类型模板形参的形参和实参间所允许的转换<br /><strong>　　　　　　1、允许从数组到指针，从函数到指针的转换。如：template &lt;int *a&gt; class A{}; int b[1]; A&lt;b&gt; m;即数组到指针的转换</strong><br /><strong>　　　　　　2、const修饰符的转换。如：template&lt;const int *a&gt; class A{}; int b; A&lt;&amp;b&gt; m;&nbsp;&nbsp; 即从int *到const int *的转换。</strong><br /><strong>　　　　　　3、提升转换。如：template&lt;int a&gt; class A{}; const short b=2; A&lt;b&gt; m; 即从short到int 的提升转换</strong><br /><strong>　　　　　　4、整值转换。如：template&lt;unsigned int a&gt; class A{};&nbsp;&nbsp; A&lt;3&gt; m; 即从int 到unsigned int的转换。</strong><br /><strong>　　　　　　5、常规转换。</strong></p>
<p><span style="color: #008000;"><strong><span style="font-size: 14pt;">非类型形参演示示例1：</span></strong></span></p>
<p>　　<strong>由用户自己亲自指定栈的大小，并实现栈的相关操作。</strong></p>


<div class="example">
<h2 class="example">TemplateDemo.h</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_HXX</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_HXX</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Stack</span><span class="hl-brackets">{</span><span class="hl-comment">//MAXSIZE由用户创建对象时自行设置</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">private</span><span class="hl-code">:
        </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">elems</span><span class="hl-brackets">[</span><span class="hl-identifier">MAXSIZE</span><span class="hl-brackets">]</span><span class="hl-code">;    </span><span class="hl-comment">// 包含元素的数组</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">numElems</span><span class="hl-code">;    </span><span class="hl-comment">// 元素的当前总个数</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-identifier">Stack</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;    </span><span class="hl-comment">//构造函数</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">push</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code">&amp;</span><span class="hl-brackets">)</span><span class="hl-code">;    </span><span class="hl-comment">//压入元素</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">pop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;        </span><span class="hl-comment">//弹出元素</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">top</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code">;    </span><span class="hl-comment">//返回栈顶元素</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-types">bool</span><span class="hl-code"> </span><span class="hl-identifier">empty</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-brackets">{</span><span class="hl-code">     </span><span class="hl-comment">// 返回栈是否为空</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">numElems</span><span class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-types">bool</span><span class="hl-code"> </span><span class="hl-identifier">full</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-brackets">{</span><span class="hl-code">    </span><span class="hl-comment">// 返回栈是否已满</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">numElems</span><span class="hl-code"> == </span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">template</span><span class="hl-code"> &lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt; 
</span><span class="hl-identifier">Stack</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">,</span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt;::</span><span class="hl-identifier">Stack</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">:</span><span class="hl-identifier">numElems</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">     </span><span class="hl-comment">// 初始时栈不含元素</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-comment">// 不做任何事情</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code"> &lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt;
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">Stack</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">, </span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt;::</span><span class="hl-identifier">push</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code">&amp; </span><span class="hl-identifier">elem</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-identifier">numElems</span><span class="hl-code"> == </span><span class="hl-identifier">MAXSIZE</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-reserved">throw</span><span class="hl-code"> </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">out_of_range</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Stack&lt;&gt;::push(): stack is full</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-identifier">elems</span><span class="hl-brackets">[</span><span class="hl-identifier">numElems</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-identifier">elem</span><span class="hl-code">;   </span><span class="hl-comment">// 附加元素</span><span class="hl-comment"></span><span class="hl-code">
    ++</span><span class="hl-identifier">numElems</span><span class="hl-code">;               </span><span class="hl-comment">// 增加元素的个数</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt;
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">Stack</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">,</span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt;::</span><span class="hl-identifier">pop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">numElems</span><span class="hl-code"> &lt;= </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-reserved">throw</span><span class="hl-code"> </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">out_of_range</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Stack&lt;&gt;::pop(): empty stack</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    --</span><span class="hl-identifier">numElems</span><span class="hl-code">;               </span><span class="hl-comment">// 减少元素的个数</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code"> &lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt;
</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">Stack</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">,</span><span class="hl-identifier">MAXSIZE</span><span class="hl-code">&gt;::</span><span class="hl-identifier">top</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-types">const</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">numElems</span><span class="hl-code"> &lt;= </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-reserved">throw</span><span class="hl-code"> </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">out_of_range</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Stack&lt;&gt;::top(): empty stack</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">elems</span><span class="hl-brackets">[</span><span class="hl-identifier">numElems</span><span class="hl-code">-</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">;  </span><span class="hl-comment">// 返回最后一个元素</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-prepro">#endif</span></div>
</div>
</div>


<div class="example">
<h2 class="example">TemplateDemo.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">string</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">cstdlib</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">Stack</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-number">20</span><span class="hl-code">&gt;  </span><span class="hl-identifier">int20Stack</span><span class="hl-code">;  </span><span class="hl-comment">// 可以存储20个int元素的栈</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">Stack</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-number">40</span><span class="hl-code">&gt;  </span><span class="hl-identifier">int40Stack</span><span class="hl-code">;  </span><span class="hl-comment">// 可以存储40个int元素的栈</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">Stack</span><span class="hl-code">&lt;</span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">string</span><span class="hl-code">,</span><span class="hl-number">40</span><span class="hl-code">&gt; </span><span class="hl-identifier">stringStack</span><span class="hl-code">; </span><span class="hl-comment">// 可存储40个string元素的栈</span><span class="hl-comment"></span><span class="hl-code">
 
        </span><span class="hl-comment">// 使用可存储20个int元素的栈</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">int20Stack</span><span class="hl-code">.</span><span class="hl-identifier">push</span><span class="hl-brackets">(</span><span class="hl-number">7</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">cout</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">int20Stack</span><span class="hl-code">.</span><span class="hl-identifier">top</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">endl</span><span class="hl-code">;    </span><span class="hl-comment">//7</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">int20Stack</span><span class="hl-code">.</span><span class="hl-identifier">pop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
        </span><span class="hl-comment">// 使用可存储40个string的栈</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">stringStack</span><span class="hl-code">.</span><span class="hl-identifier">push</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">hello</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">cout</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">stringStack</span><span class="hl-code">.</span><span class="hl-identifier">top</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">endl</span><span class="hl-code">;    </span><span class="hl-comment">//hello</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">stringStack</span><span class="hl-code">.</span><span class="hl-identifier">pop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;    
        </span><span class="hl-identifier">stringStack</span><span class="hl-code">.</span><span class="hl-identifier">pop</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;    </span><span class="hl-comment">//Exception: Stack&lt;&gt;::pop&lt;&gt;: empty stack</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">exception</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code">&amp; </span><span class="hl-identifier">ex</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">cerr</span><span class="hl-code"> &lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">Exception: </span><span class="hl-quotes">&quot;</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">ex</span><span class="hl-code">.</span><span class="hl-identifier">what</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">std</span><span class="hl-code">::</span><span class="hl-identifier">endl</span><span class="hl-code">;
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">EXIT_FAILURE</span><span class="hl-code">;  </span><span class="hl-comment">// 退出程序且有ERROR标记</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/03/2012102509100898.jpg"></p>
<h3>
非类型形参演示示例2：
</h3>
<div class="example">
<h2 class="example">TemplateDemo01.h</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_O1</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_01</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">typename</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">CompareDemo</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">compare</span><span class="hl-brackets">(</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&amp;, </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&amp;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">typename</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">CompareDemo</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;::</span><span class="hl-identifier">compare</span><span class="hl-brackets">(</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&amp; </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&amp; </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">-</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">1</span><span class="hl-code">;
    </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">-</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">&lt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> -</span><span class="hl-number">1</span><span class="hl-code">;
    </span><span class="hl-reserved">else</span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-prepro">#endif</span></div>
</div>
</div>

<div class="example">
<h2 class="example">TemplateDemo01.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo01.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">CompareDemo</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">cd</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">cd</span><span class="hl-code">.</span><span class="hl-identifier">compare</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>运行结果：<strong>-1</strong>　</p>


<div class="example">
<h2 class="example">TemplateDemo01.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo01.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">CompareDemo</span><span class="hl-code">&lt;</span><span class="hl-types">double</span><span class="hl-code">&gt; </span><span class="hl-identifier">cd</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">cd</span><span class="hl-code">.</span><span class="hl-identifier">compare</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-number">.2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-number">.1</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>运行结果： <strong>1</strong>　</p>　


<div class="example">
<h2 class="example">TemplateDemo01.h</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_O1</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_01</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">typename</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">CompareDemo</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">compare</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code">&amp;, </span><span class="hl-identifier">T</span><span class="hl-code">&amp;</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">typename</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">CompareDemo</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;::</span><span class="hl-identifier">compare</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code">&amp; </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T</span><span class="hl-code">&amp; </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">-</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">1</span><span class="hl-code">;
    </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">-</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">&lt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> -</span><span class="hl-number">1</span><span class="hl-code">;
    </span><span class="hl-reserved">else</span><span class="hl-code">
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-prepro">#endif</span></div>
</div>
</div>

<div class="example">
<h2 class="example">TempalteDemo01.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo01.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">CompareDemo</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">cd</span><span class="hl-code">;
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">=</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-identifier">b</span><span class="hl-code">=</span><span class="hl-number">3</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">cd</span><span class="hl-code">.</span><span class="hl-identifier">compare</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<h3>
非类型形参演示示例3：
</h3>
<div class="example">
<h2 class="example">TemplateDemo02.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">typename</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt;
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&amp; </span><span class="hl-identifier">max</span><span class="hl-brackets">(</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&amp; </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&amp; </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">&gt;</span><span class="hl-identifier">b</span><span class="hl-code"> ? </span><span class="hl-identifier">a</span><span class="hl-code">:</span><span class="hl-identifier">b</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">max</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-number">.1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-number">.2</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-comment">//模板实参被隐式推演成double</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">max</span><span class="hl-code">&lt;</span><span class="hl-types">double</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-number">.1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-number">.2</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-comment">//显示指定模板参数。</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">max</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-number">.1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-number">.2</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-comment">//显示指定的模板参数，会将函数函数直接转换为int。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>运行结果：</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/03/2012102515230328.jpg"></p>

<pre>
cout&lt;&lt;max&lt;int&gt;(2.1,2.2)&lt;&lt;endl; //显示指定的模板参数，会将函数函数直接转换为int。
</pre>
<p>此语句会出现警告：</p>

<pre>--------------------Configuration: TemplateDemo02 - Win32 Debug--------------------
Compiling...
TemplateDemo02.cpp
G:\C++\CDaima\TemplateDemo02\TemplateDemo02.cpp(11) : 
　　warning C4244: 'argument' : conversion from 'const double' to 'const int', possible loss of data
G:\C++\CDaima\TemplateDemo02\TemplateDemo02.cpp(11) : 
　　warning C4244: 'argument' : conversion from 'const double' to 'const int', possible loss of data

TemplateDemo02.obj - 0 error(s), 2 warning(s)</pre>

<hr><h2>
四、类模板的默认模板类型形参
</h2>
<p><strong>1、</strong><strong>可以为类模板的类型形参提供默认值，但不能为函数模板的类型形参提供默认值。</strong><strong>函数模板和类模板都可以为模板的非类型形参提供默认值。</strong></p>
<p>2、类模板的类型形参默认值形式为：<strong>template&lt;class T1, class T2=int&gt; class A{};</strong>为第二个模板类型形参<strong>T2</strong>提供<strong>int</strong>型的默认值。</p>
<p>3、类模板类型形参默认值和函数的默认参数一样，如果有多个类型形参则从第一个形参设定了默认值之后的所有模板形参都要设定默认值，比如<strong>template&lt;class T1=int, class T2&gt;class A{};</strong>就是错误的，因为<strong>T1</strong>给出了默认值，而<strong>T2</strong>没有设定。</p>
<p>4、在类模板的外部定义类中的成员时<strong>template</strong> 后的形参表应省略默认的形参类型。比如<strong>template&lt;class&nbsp; T1, class T2=int&gt; class A{public: void h();}</strong>; 定义方法为<strong>template&lt;class T1,class T2&gt; void A&lt;T1,T2&gt;::h(){}</strong>。</p>

<p><span style="color: #ff0000;"><strong>定义类模板类型形参：</strong></span></p>
<h3>
演示实例1：</h3>
<div class="example">
<h2 class="example">TemplateDemo.h</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_HXX</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_HXX</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">A</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-prepro">#endif</span></div>
</div>
</div>

<div class="example">
<h2 class="example">TemplateDemo.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;::</span><span class="hl-identifier">A</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">a</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">a</span><span class="hl-code">.</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>运行结果： <strong>5</strong></p>
<h3>
类模板的默认模板类型形参示例1：</h3>
<div class="example">
<h2 class="example">TemplateDemo03.h</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_03</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_03</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-comment">//定义带默认类型形参的类模板。这里把T2默认设置为int型。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">=</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">CeilDemo</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
</span><span class="hl-comment">//在类模板的外部定义类中的成员时template 后的形参表应省略默认的形参类型。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">&gt; 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">CeilDemo</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">&gt;::</span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">&gt;&gt;</span><span class="hl-identifier">b</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-prepro">#endif</span></div>
</div>
</div>


<div class="example">
<h2 class="example">TemplateDemo03.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo03.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">CeilDemo</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">cd</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">cd</span><span class="hl-code">.</span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-number">8</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>运行结果： 　<strong>2</strong>　</p>

<p>在类模板的外部定义类中的成员时template 后的形参表应省略默认的形参类型，如果没有省略，不会出现编译错误而是提出警告：</p>

<pre>--------------------Configuration: TemplateDemo03 - Win32 Debug--------------------
Compiling...
TemplateDemo03.cpp
g:\c++\cdaima\templatedemo03\templatedemo03.h(12) : 
warning C4519: default template arguments are only allowed on a class template; ignored

TemplateDemo03.obj - 0 error(s), 1 warning(s)</pre>
<p><strong>原作者：</strong>类模板类型形参默认值和函数的默认参数一样，如果有多个类型形参则从第一个形参设定了默认值之后的所有模板形参都要设定默认值，比如<strong>template&lt;class T1=int, class T2&gt;class A{};</strong>就是错误的，因为<strong>T1</strong>给出了默认值，而<strong>T2</strong>没有设定。</p>

<h3>类模板的默认模板类型形参示例2：</h3>

<div class="example">
<h2 class="example">TemplateDemo03.h </h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_03</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_03</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">=</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T3</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">CeilDemo</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T3</span><span class="hl-code">&gt; 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">CeilDemo</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-code">&gt;::</span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">+</span><span class="hl-identifier">c</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-prepro">#endif</span></div>
</div>
</div>

<div class="example">
<h2 class="example">TemplateDemo03.cpp </h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo03.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">CeilDemo</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">cd</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">cd</span><span class="hl-code">.</span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>运行结果：<strong>9</strong>　　</p>

<div class="example">
<h2 class="example">TemplateDemo03.h</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_03</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_03</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">=</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T3</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">CeilDemo</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T3</span><span class="hl-code">&gt; 
</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">CeilDemo</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-code">&gt;::</span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">+</span><span class="hl-identifier">c</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-prepro">#endif</span></div>
</div>
</div>

<div class="example">
<h2 class="example">TemplateDemo03.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo03.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">CeilDemo</span><span class="hl-code">&lt;</span><span class="hl-types">double</span><span class="hl-code">,</span><span class="hl-types">double</span><span class="hl-code">&gt; </span><span class="hl-identifier">cd</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">cd</span><span class="hl-code">.</span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-number">.1</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-number">.1</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>编译错误：</p>

<pre>--------------------Configuration: TemplateDemo03 - Win32 Debug--------------------
Compiling...
TemplateDemo03.cpp
g:\c++\cdaima\templatedemo03\templatedemo03.h(12) : 
error C2244: 'CeilDemo&lt;T1,T2,T3&gt;::ceil' : unable to resolve function overload
g:\c++\cdaima\templatedemo03\templatedemo03.cpp(6) : 
error C2065: 'cd' : undeclared identifier
g:\c++\cdaima\templatedemo03\templatedemo03.cpp(6) : 
error C2228: left of '.ceil' must have class/struct/union type
Error executing cl.exe.

TemplateDemo03.obj - 3 error(s), 0 warning(s)</pre>


<p>从上面的例子我们可以看出，当我们试图把T2和T3定义为double类型就会出现错误（T1默认定义的是int类型）。那是不是我们按照作者所说把T2和T3也设定为默认值double，是否还会出现错误？看下面的示例：</p>

<h3>
类模板的默认模板类型形参示例4：
</h3>
<div class="example">
<h2 class="example">TemplateDemo03.h</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#ifndef</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_03</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">TEMPLATE_DEMO_03</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">=</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">=</span><span class="hl-types">double</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T3</span><span class="hl-code">=</span><span class="hl-types">double</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">CeilDemo</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T3</span><span class="hl-code">&gt; 
</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">CeilDemo</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-code">&gt;::</span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">+</span><span class="hl-identifier">c</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-prepro">#endif</span></div>
</div>
</div>

<div class="example">
<h2 class="example">TemplateDemo03.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">TemplateDemo03.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">CeilDemo</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-types">double</span><span class="hl-code">,</span><span class="hl-types">double</span><span class="hl-code">&gt; </span><span class="hl-identifier">cd</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">cd</span><span class="hl-code">.</span><span class="hl-identifier">ceil</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-number">.1</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-number">.1</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>编译错误：</p>

<pre>--------------------Configuration: TemplateDemo03 - Win32 Debug--------------------
Compiling...
TemplateDemo03.cpp
g:\c++\cdaima\templatedemo03\templatedemo03.h(12) : 
error C2244: 'CeilDemo&lt;T1,T2,T3&gt;::ceil' : unable to resolve function overload
g:\c++\cdaima\templatedemo03\templatedemo03.cpp(6) : 
error C2065: 'cd' : undeclared identifier
g:\c++\cdaima\templatedemo03\templatedemo03.cpp(6) : 
error C2228: left of '.ceil' must have class/struct/union type
Error executing cl.exe.

TemplateDemo03.obj - 3 error(s), 0 warning(s)</pre>

<p>从结果我们可以看出，和上例是一样的错误。从实例中我们可以总结如下：类模板如果有多个类型形参，如果使用类型形参默认值则尽量放在参数列表的末尾，而且默认的参数类型必须相同。如果从第一个形参设定了默认值之后的所有模板形参都要设定和第一个形参同类型的默认值。<strong>（声明：本人也是刚接触C++，以上只是我经过实例演示对原作者提出的一些质疑，可能我的示例有不到之处，还望大神们不吝赐教，共同完善此博客，给像我一样的菜鸟提供一个学习的平台！）</strong></p>

<p>接下来验证"不能为函数模板的类型形参提供默认值"：</p>
<h3>
类模板的默认模板类型形参示例5：
</h3>
<div class="example">
<h2 class="example">TemplateDemo04.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T3</span><span class="hl-code">&gt;
</span><span class="hl-identifier">T1</span><span class="hl-code"> </span><span class="hl-identifier">sum</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code">=</span><span class="hl-types">int</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">+</span><span class="hl-identifier">c</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code"> 
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">sum</span><span class="hl-code">&lt;</span><span class="hl-types">double</span><span class="hl-code">,</span><span class="hl-types">double</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-number">.1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-number">.1</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>编译错误：</p>

<pre>--------------------Configuration: TemplateDemo04 - Win32 Debug--------------------
Compiling...
TemplateDemo04.cpp
g:\c++\cdaima\templatedemo04\templatedemo04.cpp(4) : 
error C2062: type 'int' unexpected
Error executing cl.exe.

TemplateDemo04.obj - 1 error(s), 0 warning(s)</pre>

<p>更改之后的 TemplateDemo.cpp</p>


<div class="example">
<h2 class="example">TemplateDemo.cpp</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T3</span><span class="hl-code">&gt;
</span><span class="hl-identifier">T1</span><span class="hl-code"> </span><span class="hl-identifier">sum</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">,</span><span class="hl-identifier">T3</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">+</span><span class="hl-identifier">c</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code"> 
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">sum</span><span class="hl-code">&lt;</span><span class="hl-types">double</span><span class="hl-code">,</span><span class="hl-types">short</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-number">.1</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">257</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>

<p>运行结果：　　　<strong>261.1</strong>　</p>　　　　

<p>原作者演示实例如下：</p>
<div class="example">
<h2 class="example">类模板非类型形参示例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-comment">//模板的声明或定义只能在全局，命名空间或类范围内进行。即不能在局部范围，函数内进行，比如不能在main函数中声明或定义一个模板。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//类模板的定义</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-brackets">{</span><span class="hl-reserved">public</span><span class="hl-code">:</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-identifier">A</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">;  </span><span class="hl-comment">//定义带有一个类模板类型形参T的类A</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">&gt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">B</span><span class="hl-brackets">{</span><span class="hl-reserved">public</span><span class="hl-code">:</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">; </span><span class="hl-comment">//定义带有两个类模板类型形参T1，T2的类B</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//定义类模板的默认类型形参，默认类型形参不适合于函数模板。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">=</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">D</span><span class="hl-brackets">{</span><span class="hl-reserved">public</span><span class="hl-code">: </span><span class="hl-identifier">voidg</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">; </span><span class="hl-comment">//定义带默认类型形参的类模板。这里把T2默认设置为int型。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//template&lt;class T1=int, class T2&gt;class E{}; //错误，为T1设了默认类型形参则T1后面的所有形参都必须设置认默值。</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-comment">//以下为非类型形参的定义</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//非类型形参只能是整型，指针和引用，像double，String, String **这样的类型是不允许的。但是double &amp;，double *对象的引用或指</span><span class="hl-comment"></span><span class="hl-code">
针是正确的。
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Ci</span><span class="hl-brackets">{</span><span class="hl-reserved">public</span><span class="hl-code">:</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">; </span><span class="hl-comment">//定义模板的非类型形参，形参为整型</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> &amp;</span><span class="hl-identifier">a</span><span class="hl-code">&gt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Cip</span><span class="hl-brackets">{</span><span class="hl-reserved">public</span><span class="hl-code">:</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">; 
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt;* </span><span class="hl-identifier">m</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Cc</span><span class="hl-brackets">{</span><span class="hl-reserved">public</span><span class="hl-code">:</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">; </span><span class="hl-comment">//定义模板的模板类型形参，形参为int型的类A的对象的指针。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">double</span><span class="hl-code">*</span><span class="hl-identifier">a</span><span class="hl-code">&gt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Cd</span><span class="hl-brackets">{</span><span class="hl-reserved">public</span><span class="hl-code">:</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">;  </span><span class="hl-comment">//定义模板的非类型形参，形参为double类型的引用。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">E</span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">; </span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">E</span><span class="hl-code"> &amp;</span><span class="hl-identifier">m</span><span class="hl-code">&gt; </span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Ce</span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">; </span><span class="hl-comment">//非类型模板形参为对象的引用。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//以下非类型形参的声明是错误的。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//template&lt;class T1,A m&gt;class Cc{}; //错误，对象不能做为非类型形参，非类型模板形参的类型只能是对象的引用或指针。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//template&lt;class T1,double a&gt;class Cc{}; //错误，非类型模板的形参不能是double类型，可以是double的引用。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//template&lt;class T1,A&lt;int&gt; m&gt;class Cc{}; //错误，非类型模板的形参不能是对象，必须是对象的引用或指针。这条规则对于模板型参</span><span class="hl-comment"></span><span class="hl-code">
也不例外。
</span><span class="hl-comment">//在类模板外部定义各种类成员的方法，</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//typeid(变量名).name()的作用是提取变量名的类型，如int a，则cout&lt;&lt;typeid(a).name()将输出int</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt;   </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;::</span><span class="hl-identifier">A</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">class A goucao</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-reserved">typeid</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">//在类模板外部定义类的构造函数的方法</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">class A g(T a,T b)</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-comment">//在类模板外部定义类模板的成员</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">&gt;  </span><span class="hl-identifier">voidB</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">class g f()</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-reserved">typeid</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-reserved">typeid</span><span class="hl-brackets">(</span><span class="hl-identifier">T2</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//在类外面定义类的成员时template后面的模板形参应与要定义的类的模板形参一致</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">&gt;     </span><span class="hl-identifier">voidCi</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">a</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">class Ci g()</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-reserved">typeid</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code"> &amp;</span><span class="hl-identifier">a</span><span class="hl-code">&gt;    </span><span class="hl-identifier">voidCip</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">a</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">class Cip g()</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-reserved">typeid</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code"> 
</span><span class="hl-comment">//在类外部定义类的成员时，template后的模板形参应与要定义的类的模板形参一致</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; *</span><span class="hl-identifier">m</span><span class="hl-code">&gt; </span><span class="hl-identifier">voidCc</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">m</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">class Cc g()</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-reserved">typeid</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">double</span><span class="hl-code">* </span><span class="hl-identifier">a</span><span class="hl-code">&gt; </span><span class="hl-identifier">voidCd</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">a</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">class Cd g()</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-reserved">typeid</span><span class="hl-brackets">(</span><span class="hl-identifier">T1</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">name</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-comment">//带有默认类型形参的模板类，在类的外部定义成员的方法。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//在类外部定义类的成员时，template的形参表中默认值应省略</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">template</span><span class="hl-code">&lt;</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">T2</span><span class="hl-code">&gt;  </span><span class="hl-identifier">voidD</span><span class="hl-code">&lt;</span><span class="hl-identifier">T1</span><span class="hl-code">,</span><span class="hl-identifier">T2</span><span class="hl-code">&gt;::</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">class D g()</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//template&lt;class T1,class T2=int&gt; void D&lt;T1,T2&gt;::g(){cout&lt;&lt;&quot;class D k()&quot;&lt;&lt;endl;} //错误，在类模板外部定义带有默认类型的形</span><span class="hl-comment"></span><span class="hl-code">
参时，在</span><span class="hl-types">template</span><span class="hl-code">的形参表中默认值应省略。
</span><span class="hl-comment">//定义一些全局变量。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">e</span><span class="hl-code">=</span><span class="hl-number">2</span><span class="hl-code">;  </span><span class="hl-identifier">doubleed</span><span class="hl-code">=</span><span class="hl-number">2</span><span class="hl-number">.2</span><span class="hl-code">; </span><span class="hl-types">double</span><span class="hl-code">*</span><span class="hl-identifier">pe</span><span class="hl-code">=&amp;</span><span class="hl-identifier">ed</span><span class="hl-code">;
</span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">mw</span><span class="hl-code">; </span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; *</span><span class="hl-identifier">pec</span><span class="hl-code">=&amp;</span><span class="hl-identifier">mw</span><span class="hl-code">; </span><span class="hl-identifier">E</span><span class="hl-code"> </span><span class="hl-identifier">me</span><span class="hl-code">;
 
</span><span class="hl-comment">//main函数开始</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">// template&lt;class T&gt;void h(){} //错误，模板的声明或定义只能在全局，命名空间或类范围内进行。即不能在局部范围，函数内进行。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//A&lt;2&gt; m; //错误，对类模板不存在实参推演问题，类模板必须在尖括号中明确指出其类型。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//类模板调用实例</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">A</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">ma</span><span class="hl-code">; </span><span class="hl-comment">//输出&quot;class A goucao int&quot;创建int型的类模板A的对象ma。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">B</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-types">int</span><span class="hl-code">&gt; </span><span class="hl-identifier">mb</span><span class="hl-code">; </span><span class="hl-identifier">mb</span><span class="hl-code">.</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//输出&quot;class B g() int int&quot;创建类模板B的对象mb，并把类型形参T1和T2设计为int</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//非类型形参的调用</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//调用非类型模板形参的实参必须是一个常量表达式，即他必须能在编译时计算出结果。任何局部对象，局部变量，局部对象的地址，局部</span><span class="hl-comment"></span><span class="hl-code">
变量的地址都不是一个常量表达式，都不能用作非类型模板形参的实参。全局指针类型，全局变量，全局对象也不是一个常量表达式，不能
用作非类型模板形参的实参。
</span><span class="hl-comment">//全局变量的地址或引用，全局对象的地址或引用const类型变量是常量表达式，可以用作非类型模板形参的实参。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//调用整型int型非类型形参的方法为名为Ci，声明形式为template&lt;class T1,int a&gt; class Ci</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">Ci</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">&gt;</span><span class="hl-comment">//正确，数值R是一个int型常量，输出&quot;class Ci g() int&quot;</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a2</span><span class="hl-code">=</span><span class="hl-number">3</span><span class="hl-code">; </span><span class="hl-identifier">Ci</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-identifier">a2</span><span class="hl-code">&gt; </span><span class="hl-identifier">mci1</span><span class="hl-code">; </span><span class="hl-identifier">mci1</span><span class="hl-code">.</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//正确，因为a2在这里是const型的常量。输出&quot;class Ci g() int&quot;</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//Ci&lt;int,a&gt; mci; //错误，int型变量a是局部变量，不是一个常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//Ci&lt;int,e&gt; mci; //错误，全局int型变量e也不是一个常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//调用int&amp;型非类型形参的方法类名为Cip，声明形式为template&lt;class T1,int &amp;a&gt;class Cip</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">Cip</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-identifier">e</span><span class="hl-code">&gt; </span><span class="hl-identifier">mcip</span><span class="hl-code">;  </span><span class="hl-comment">//正确，对全局变量的引用或地址是常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//Cip&lt;int,a&gt; mcip1; //错误，局部变量的引用或地址不是常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//调用double*类型的非类形形参类名为Cd，声明形式为template&lt;class T1,double *a&gt;class Cd</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">Cd</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,&amp;</span><span class="hl-identifier">ed</span><span class="hl-code">&gt; </span><span class="hl-identifier">mcd</span><span class="hl-code">; </span><span class="hl-comment">//正确，全局变量的引用或地址是常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//Cd&lt;int,pe&gt; mcd1; //错误，全局变量指针不是常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//double dd=3.3; //错误，局部变量的地址不是常量表达式，不能用作非类型形参的实参</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//Cd&lt;int,&amp;e&gt; mcd;  //错误，非类型形参虽允许一些转换，但这个转换不能实现。</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-comment">//调用模板类型形参对象A&lt;int&gt; *的方法类名为Cc，声名形式为template&lt;class T1,A&lt;int&gt;* m&gt; class Cc</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">Cc</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,&amp;</span><span class="hl-identifier">mw</span><span class="hl-code">&gt; </span><span class="hl-identifier">mcc</span><span class="hl-code">; </span><span class="hl-identifier">mcc</span><span class="hl-code">.</span><span class="hl-identifier">g</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//正确，全局对象的地址或者引用是常量表达式</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//Cc&lt;int,&amp;ma&gt; mcc;  //错误，局部变量的地址或引用不是常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//Cc&lt;int,pec&gt; mcc2;  //错误，全局对象的指针不是常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-comment">//调用非类型形参E&amp;对象的引用的方法类名为Ce。声明形式为template&lt;class T1,E &amp;m&gt; class Ce</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">E</span><span class="hl-code"> </span><span class="hl-identifier">me1</span><span class="hl-code">; </span><span class="hl-comment">//Ce&lt;int,me1&gt; mce1; //错误，局部对象不是常量表达式</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">Ce</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-identifier">me</span><span class="hl-code">&gt; </span><span class="hl-identifier">mce</span><span class="hl-code">;  </span><span class="hl-comment">//正确，全局对象的指针或引用是常量表达式。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//非类型形参的转换示例，类名为Ci</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//非类型形参允许从数组到指针，从函数到指针的转换，const修饰符的转换，提升转换，整值转换，常规转换。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">short</span><span class="hl-code"> </span><span class="hl-identifier">s</span><span class="hl-code">=</span><span class="hl-number">3</span><span class="hl-code">; </span><span class="hl-identifier">Ci</span><span class="hl-code">&lt;</span><span class="hl-types">int</span><span class="hl-code">,</span><span class="hl-identifier">s</span><span class="hl-code">&gt; </span><span class="hl-identifier">mci4</span><span class="hl-code"></span><span class="hl-comment">//正确，虽然short型和int不完全匹配，但这里可以将short型转换为int型</span></div>
</div>
</div>



<blockquote><p>原文地址：</p>
<p>https://www.cnblogs.com/gw811/archive/2012/10/25/2738929.html</p>
<p>http://www.cnblogs.com/gw811/archive/2012/10/25/2736224.html</p></blockquote>

				</div>
			</div>
			<div class="previous-next-links">
			<div class="previous-design-link">← <a href="../w3cnote/cpp-two-colon-usage.html" rel="prev"> C++ 中 :: 的用法</a> </div>
			<div class="next-design-link"><a href="../w3cnote/macro-definition.html" rel="next"> 宏定义的正确写法，三目运算的宏定义</a> →</div>
			</div>
						<div class="article-heading-ad" id="w3cnote-ad728">
			<script async src=""></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="horizontal"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
			</div>
			<style>
@media screen and (max-width: 768px) {
	#w3cnote-ad728 {
		display: none;
	}
}
p.note-author {
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    font-weight: bold;
    color: #78a15a;
    padding-bottom: 2px;
    margin-bottom: 4px;
}
</style>
<script>
var aid = 19684;
</script>
	</div>
		
	</div>
	<div class="listcol last right-column">




<!--
	<div class="tab tab-light-blue"> 订阅</div>
	<div class="sidebar-box">
		<div class="socialicons">
			<a href="../feed" class="rss">RSS 订阅</a>
		
			<form action="//list.qq.com/cgi-bin/qf_compose_send" method="post">
			<input type="hidden" value="qf_booked_feedback" name="t">
			<input type="hidden" value="4b67b6b6c1f5e792559940cab4aebb8f1126fba880bff1a8" name="id">
			<input class="placeholder" id="feed_email" name="to" value="输入邮箱 订阅笔记" autocomplete="off">
			<input type="submit" value="订阅" class="btn btn-primary">
			</form>
		
		</div>
 
	</div>
-->	



<!--
	<div class="sidebar-box cate-list">
	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">笔记列表</a>
		</div>

 

</div>
-->

	 <div class="sidebar-box cate-list">
		 		

	 	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">教程列表</a>
		</div>
		
		<div class="cate-items"> 
				<a href="../ado">ADO 教程</a>
	<a href="../ajax">Ajax 教程</a>
	<a href="../android">Android 教程</a>
	<a href="../angularjs2">Angular2 教程</a>
	<a href="../angularjs">AngularJS 教程</a>
	<a href="../appml">AppML 教程</a>
	<a href="../asp">ASP 教程</a>
	<a href="../aspnet">ASP.NET 教程</a>
	<a href="../bootstrap">Bootstrap 教程</a>
	<a href="../bootstrap4">Bootstrap4 教程</a>
	<a href="../bootstrap5">Bootstrap5 教程</a>
	<a href="../cprogramming">C 教程</a>
	<a href="../csharp">C# 教程</a>
	<a href="../cplusplus">C++ 教程</a>
	<a href="../chartjs">Chart.js 教程</a>
	<a href="../cssref">CSS 参考手册</a>
	<a href="../css">CSS 教程</a>
	<a href="../css3">CSS3 教程</a>
	<a href="../django">Django 教程</a>
	<a href="../docker">Docker 教程</a>
	<a href="../dtd">DTD 教程</a>
	<a href="../echarts">ECharts 教程</a>
	<a href="../eclipse">Eclipse 教程</a>
	<a href="../firebug">Firebug 教程</a>
	<a href="../font-awesome">Font Awesome 图标</a>
	<a href="../foundation">Foundation 教程</a>
	<a href="../git">Git 教程</a>
	<a href="../go">Go 语言教程</a>
	<a href="../googleapi">Google 地图 API 教程</a>
	<a href="../highcharts">Highcharts 教程</a>
	<a href="../htmldom">HTML DOM 教程</a>
	<a href="../tags">HTML 参考手册</a>
	<a href="../charsets">HTML 字符集</a>
	<a href="../html">HTML 教程</a>
	<a href="../http">HTTP 教程</a>
	<a href="../ionic">ionic 教程</a>
	<a href="../ios">iOS 教程</a>
	<a href="../java">Java 教程</a>
	<a href="../jsref">JavaScript 参考手册</a>
	<a href="../js">Javascript 教程</a>
	<a href="../jeasyui">jQuery EasyUI 教程</a>
	<a href="../jquerymobile">jQuery Mobile 教程</a>
	<a href="../jqueryui">jQuery UI 教程</a>
	<a href="../jquery">jQuery 教程</a>
	<a href="../json">JSON 教程</a>
	<a href="../jsp">JSP 教程</a>
	<a href="../julia">Julia 教程</a>
	<a href="../kotlin">Kotlin 教程</a>
	<a href="../linux">Linux 教程</a>
	<a href="../lua">Lua 教程</a>
	<a href="../markdown">Markdown 教程</a>
	<a href="../matplotlib">Matplotlib 教程</a>
	<a href="../maven">Maven 教程</a>
	<a href="../memcached">Memcached 教程</a>
	<a href="../mongodb">MongoDB 教程</a>
	<a href="../mysql">MySQL 教程</a>
	<a href="../nodejs">Node.js 教程</a>
	<a href="../numpy">NumPy 教程</a>
	<a href="../pandas">Pandas 教程</a>
	<a href="../perl">Perl 教程</a>
	<a href="../php">PHP 教程</a>
	<a href="../postgresql">PostgreSQL 教程</a>
	<a href="../python3">Python 3 教程</a>
	<a href="../python">Python 基础教程</a>
	<a href="../r">R 教程</a>
	<a href="../rdf">RDF 教程</a>
	<a href="../react">React 教程</a>
	<a href="../redis">Redis 教程</a>
	<a href="../rss">RSS 教程</a>
	<a href="../ruby">Ruby 教程</a>
	<a href="../rust">Rust 教程</a>
	<a href="../sass">Sass 教程</a>
	<a href="../scala">Scala 教程</a>
	<a href="../scipy">SciPy 教程</a>
	<a href="../servlet">Servlet 教程</a>
	<a href="../soap">SOAP 教程</a>
	<a href="../sql">SQL 教程</a>
	<a href="../sqlite">SQLite 教程</a>
	<a href="../svg">SVG 教程</a>
	<a href="../svn">SVN 教程</a>
	<a href="../swift">Swift 教程</a>
	<a href="../tcpip">TCP/IP 教程</a>
	<a href="../typescript">TypeScript 教程</a>
	<a href="../vbscript">VBScript 教程</a>
	<a href="../vue2">Vue.js 教程</a>
	<a href="../vue3">Vue3 教程</a>
	<a href="../w3c">W3C 教程</a>
	<a href="../webservices">Web Service 教程</a>
	<a href="../wsdl">WSDL 教程</a>
	<a href="../xlink">XLink 教程</a>
	<a href="../dom">XML DOM 教程</a>
	<a href="../schema">XML Schema 教程</a>
	<a href="../xml">XML 教程</a>
	<a href="../xpath">XPath 教程</a>
	<a href="../xquery">XQuery 教程</a>
	<a href="../xslfo">XSLFO 教程</a>
	<a href="../xsl">XSLT 教程</a>
	<a href="../data-structures">数据结构</a>
	<a href="../regexp">正则表达式</a>
	<a href="../quiz">测验</a>
	<a href="../browsers">浏览器</a>
	<a href="../quality">网站品质</a>
	<a href="../web">网站建设指南</a>
	<a href="../hosting">网站服务器教程</a>
	<a href="../design-pattern">设计模式</a>
			
		</div> 
		 	 </div>
</div>
	</div>
</div>


<!-- 底部 -->
<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a target="_blank" href="../html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a target="_blank" href="../jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a target="_blank" href="../charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6939/">JS 混淆/加密</a>
      </dd> 
      <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6232/">PNG/JPEG 图片压缩</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/6680/">随机数生成器</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imread.html" title="Matplotlib imread() 方法">Matplotlib imre...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imsave.html" title="Matplotlib imsave() 方法">Matplotlib imsa...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imshow.html" title="Matplotlib imshow() 方法">Matplotlib imsh...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-hist.html" title="Matplotlib 直方图">Matplotlib 直方图</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-func-object.html" title="Python object() 函数">Python object()...</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-ai-draw.html" title="Python AI 绘画">Python AI 绘画</a>
      </dd>
              <dd>
       &middot;
      <a href="../w3cnote/cursor-editor.html" title="神辅助 Cursor 编辑器，加入 GPT-4 让编码更轻松！">神辅助 Cursor ...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="mailto:admin@runoob.com" rel="external nofollow">意见反馈</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a target="_blank" href="../aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png" />
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2023    <strong><a href="../" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="../" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="display:none" href="javascript:void(0)"   title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png"></div> 
    </div>
  </div>

 <div style="display:none;">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-84264393-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>

<script src=""></script>
<script src=""></script>

</body>
</html>